#import "ALEditTemplateColumnEntry.h"
#import "ALElement.h"
#import "SMRShadowTextField.h"
#import "NSObject+BlockObservation.h"


static const NSInteger ALEditTemplateColumnEntryLabelTag = 1;

@interface ALEditTemplateColumnEntry (ALEditTemplateColumnEntryPrivateMethods)
-(NSColor *)highlightedColor_;
-(NSColor *)normalColor_;
-(NSColor *)editColor_;
@end

@implementation ALEditTemplateColumnEntry

-(void)setView:(NSView *)view {
	[super setView:view];
	SMRShadowTextField *label = (SMRShadowTextField *)[view viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setDelegate:self];
	
	AMBlockTask fontTask = ^(id object, NSDictionary *change) {
		NSSize minSize = [[label cell] cellSize];
		NSRect labelFrame = [label frame];
		labelFrame.size.height = minSize.height;
		CGFloat viewHeight = [[self view] frame].size.height;
		labelFrame.origin.y = (viewHeight - labelFrame.size.height) / 2;
		labelFrame = [[self view] centerScanRect:labelFrame];
		[label setFrame:labelFrame];
	};
	[label addObserverForKeyPath:NSFontBinding task:fontTask];
//	[label addObserver:self forKeyPath:NSFontBinding options:NSKeyValueObservingOptionNew context:NULL];
	
	
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	// NSFontBinding for label text field
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	NSSize minSize = [[label cell] cellSize];
	NSRect labelFrame = [label frame];
	labelFrame.size.height = minSize.height;
	CGFloat viewHeight = [[self view] frame].size.height;
	labelFrame.origin.y = (viewHeight - labelFrame.size.height) / 2;
	labelFrame = [[self view] centerScanRect:labelFrame];
	[label setFrame:labelFrame];
}

+(NSSet *)keyPathsForValuesAffectingElementIsPrimary {
	return [NSSet setWithObject:[NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALLineNumberKey]];
}

// TODO: REPLACE THIS WITH THE ELEMENT'S PRIMARY METHOD
-(BOOL)elementIsPrimary {
	if ([[(ALElement *)[self representedObject] lineNumber] integerValue] == 0)
		return YES;
	return NO;
}

+(NSSet *)keyPathsForValuesAffectingEditTemplateLabelColor {
	return [NSSet setWithObject:@"columnView.selectedEntries"];
}

-(NSColor *)editTemplateLabelColor {
	if ([self ancestorIsSelected]) 
		return [self highlightedColor_];
	return [self normalColor_];
}

+(NSSet *)keyPathsForValuesAffectingElementLabelFontSize {
	return [NSSet setWithObject:ALRepresentedObjectKey];
}

-(NSInteger)elementLabelFontSize {
	if ([self elementIsPrimary])
		return ALElementFontSizePrimary;
	return ALElementFontSizeSecondary;
}


#pragma mark -
#pragma mark Editing

-(void)columnEntryDidBeginEditingAtPoint:(NSPoint)aPoint {
	[super columnEntryDidBeginEditingAtPoint:aPoint];
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setNormalColor:[self highlightedColor_]];
	[label setEditingColor:[self editColor_]];
	[label setEditable:YES];
	[label setNextKeyView:[self columnView]];
	[[label window] makeFirstResponder:label];
	[[label window] invalidateCursorRectsForView:label];
}

-(void)columnEntryDidEndEditing {
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setEditable:NO];
}

-(NSColor *)highlightedColor_ {
	return [NSColor whiteColor];
}

-(NSColor *)normalColor_ {
	return [self elementIsPrimary] ? [NSColor colorWithCalibratedWhite:0.5f alpha:1.0f] : [NSColor colorWithCalibratedWhite:0.4f alpha:1.0f];
}

-(NSColor *)editColor_ {
	return [NSColor blackColor];
}


#pragma mark -
#pragma mark Text field delegate

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
	SMRShadowTextField *label = [[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	if (control != label)
		return NO;
	if (command == @selector(cancelOperation:)) {
		[self discardEditing];
		NSWindow *controlWindow = [control window];
		[controlWindow makeFirstResponder:[self columnView]];
		return YES;
	}
	return NO;
}

-(void)controlTextDidEndEditing:(NSNotification *)aNotification {
	SMRShadowTextField *label = [[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[[label cell] endEditing:nil];
	[[self columnView] stopEditing];
	[[label window] makeFirstResponder:[self columnView]];
}

@end
